import unittest
import requests
from api.AccountApi import OpenAccountApi
from api.RegLoginApi import RegLoginApi
from api.TenderApi import TenderApi
from api.recharge_api import RechargeApi
from common import utils_p2p
from common.utils_p2p import init_tender_info, html_util
from parameterized import parameterized


class TestTender(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        # 借款标测试数据初始化
        init_tender_info()

    def setUp(self) -> None:
        # 创建session对象
        self.session = requests.session()
        # 调用投资模块类
        self.tender_api = TenderApi(self.session)
        # 调用登录模块类
        self.login_api = RegLoginApi(self.session)
        # 调用开户模块类
        self.account_api = OpenAccountApi(self.session)
        # 调用充值模块类
        self.reg_recharge_api = RechargeApi(self.session)

    def tearDown(self) -> None:
        # 关闭session对象
        self.session.close()

    # 投资测试用例
    @parameterized.expand(utils_p2p.read_json("tender_data", "tender_info"))
    def test01_tender(self, phone, password, id, amount, exp_status_code, exp_status, exp_data):
        # 用户登录
        self.login_api.user_login(phone, password)
        # 投资
        response = self.tender_api.tender(id, amount)
        # 断言
        utils_p2p.common_assert(self, response, exp_status_code, exp_status, exp_data)

    # 第三方投资测试用例
    def test02_third_tender(self):
        # 登录
        self.login_api.user_login("13422221111", "123abc")
        # 投资
        res = self.tender_api.tender(642, 500)
        request_data = utils_p2p.html_util(res)
        # 获取第三方投资接口的url
        url = request_data[0]
        # 获取第三方投资接口的请求体数据
        from_dict = request_data[1]
        # 获取第三方接口返回的响应结果
        res_3_recharge = self.tender_api.third_tender(url, from_dict)
        # 断言
        self.assertEqual(200, res_3_recharge.status_code)
        self.assertIn("OK", res_3_recharge.text)

    # 投资业务流程
    def test03_tender_process(self):
        pass
        # 将使用的数据，设置为变量，方便进行统一管理

        # 获取图片验证码接口参数
        picture_r = 1234566

        # 发送短信接口参数
        phone = "13691645121"
        imgVerifyCode = "8888"

        # 注册接口参数
        register_password = "123abc"
        register_imgVerifyCode = "8888"
        register_phone_code = "666666"

        # 登录接口参数
        login_password = "123abc"

        # 实名认证接口参数
        user_name = "张三"
        user_id = "632323190605260500"

        # 充值获取验证码接口参数
        recharge_r = "12312345"
        # 充值接口参数
        recharge_money = "20000"
        recharge_code = "8888"
        # 投资接口参数
        investment_id = 642
        investment_amount = 500
        # 1.图片验证码接口
        # 通过调用封装的图片验证码接口的方法，获取响应结果
        picture_response = self.login_api.get_img_verify_code(picture_r)
        print(f"获取图片验证码结果为：{picture_response}")
        # 断言状态码
        self.assertEqual(200, picture_response.status_code)
        # 2.获取短信验证码接口
        # 通过调用封装的短信验证码接口的方法，获取响应结果
        message_response = self.login_api.get_phone_verify_code(phone_v=phone, imgVerifyCode_v=imgVerifyCode,
                                                                type_v='reg')
        print(f"获取短信验证码结果为：{message_response}")
        # 断言-通用断言
        utils_p2p.common_assert(self, message_response, 200, 200, "短信发送成功")

        # 3.注册接口
        # 通过调用封装的注册接口的方法，获取响应结果
        register_response = self.login_api.user_register(phone_v=phone, password_v=register_password,
                                                         verifycode_v=register_imgVerifyCode,
                                                         phone_code_v=register_phone_code, dy_server="on")
        resp_dict = register_response.json()
        print(f"注册结果为：{resp_dict}")
        # 断言-通用断言
        utils_p2p.common_assert(self, register_response, 200, 200, "注册成功")

        # 4.登录接口
        # 通过调用封装的登录接口的方法，获取响应结果
        login_response = self.login_api.user_login(keywords_v=phone, password_v=login_password)
        login_resp_dict = login_response.json()
        print(f"登录结果为：{login_resp_dict}")
        # 断言-通用断言
        utils_p2p.common_assert(self, login_response, 200, 200, "登录成功")

        # 5.实名认证接口
        # 通过调用封装的实名认证接口的方法，获取响应结果
        real_name_response = self.account_api.realname(user_name, user_id)
        real_resp_dict = real_name_response.json()
        print(f"登录结果为：{real_resp_dict}")
        # 断言-通用断言
        utils_p2p.common_assert(self, real_name_response, 200, 200, "提交成功")
        # 实名认证信息断言
        self.assertEqual("632****500", real_resp_dict.get("data").get("card_id"))
        self.assertEqual("张**", real_resp_dict.get("data").get("realname"))

        # 6.开户接口
        # 通过调用封装的开户接口的方法，获取响应结果
        open_account_response = self.account_api.open_account()
        open_account_data = open_account_response
        # 断言-通用断言
        utils_p2p.common_assert(self, open_account_response, 200, 200, 'form')

        # 7.第三方开户接口
        # 通过调用封装的获取第三方接口请求数据的方法，获取第三方开户接口的url和请求体数据
        open_account_value = html_util(open_account_data)
        open_account_url = open_account_value[0]
        open_account_info = open_account_value[1]
        # 通过调用封装的第三方开户接口的方法，传入url和请求体数据，获取响应结果
        third_open_account_response = self.account_api.third_open_account(open_account_url, open_account_info)
        # 断言状态码
        self.assertEqual(200, third_open_account_response.status_code)
        # 断言响应体数据
        self.assertIn("OK", third_open_account_response.text)

        # 8.获取充值验证码接口
        # 通过调用封装的获取充值验证码接口的方法，获取响应结果
        recharge_verification_response = self.reg_recharge_api.get_recharge_verify_code(recharge_r)
        # 断言
        # 断言状态码
        self.assertEqual(200, recharge_verification_response.status_code)

        # 9.充值接口
        # 通过调用封装的充值接口的方法，获取响应结果
        recharge_response = self.reg_recharge_api.recharge(amount_v=recharge_money, valicode_v=recharge_code)
        # 断言-通用断言
        utils_p2p.common_assert(self, recharge_response, 200, 200, "form")

        # 10.第三方充值接口
        # 通过调用封装的获取第三方接口请求数据的方法，获取第三方开户接口的url和请求体数据
        third_party_recharge_date = utils_p2p.html_util(recharge_response)
        third_party_recharge_url = third_party_recharge_date[0]
        third_party_recharge_from_dict = third_party_recharge_date[1]
        # 通过调用封装的第三方充值接口的方法，传入url和请求体数据，获取响应结果
        third_party_recharge_response = self.reg_recharge_api.third_recharge(third_party_recharge_url, third_party_recharge_from_dict)
        # 断言状态码
        self.assertEqual(200, third_party_recharge_response.status_code)
        # 断言响应体数据
        self.assertIn("OK", third_party_recharge_response.text)
        # 11.投资接口
        # 通过调用封装的投资接口的方法，获取响应结果
        investment_response = self.tender_api.tender(investment_id, investment_amount)
        # 断言-通用断言
        utils_p2p.common_assert(self, investment_response, 200, 200, "form")

        # 12.第三方投资接口
        # 通过调用封装的获取第三方接口请求数据的方法，获取第三方开户接口的url和请求体数据
        third_party_investment_date = utils_p2p.html_util(investment_response)
        third_party_investment_url = third_party_investment_date[0]
        third_party_investment_from_dict = third_party_investment_date[1]
        # 通过调用封装的第三方投资接口的方法，传入url和请求体数据，获取响应结果
        third_party_request_info = self.tender_api.third_tender(third_party_investment_url, third_party_investment_from_dict)
        # 断言状态码
        self.assertEqual(200, third_party_request_info.status_code)
        # 断言响应体数据
        self.assertIn("OK", third_party_request_info.text)
